package edu.istu.asu07_1.live.gui;

import java.awt.Color;
import java.awt.Font;

import javax.naming.TimeLimitExceededException;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import edu.istu.asu07_1.live.configs.Constants;
import edu.istu.asu07_1.live.model.Taktable;
import edu.istu.asu07_1.live.model.agents.Agent;
import edu.istu.asu07_1.live.model.agents.Herb;
import edu.istu.asu07_1.live.model.agents.Herbivore;
import edu.istu.asu07_1.live.model.agents.Predator;

public class NotifyPanel implements Taktable{

	private JPanel content;
	private JLabel logo;
	private JLabel type;
	private JLabel generation;
	private JLabel energy;
	private JLabel liveTime;
	private ImageIcon herb;
	private ImageIcon herbivore;
	private ImageIcon predator;
	private JLabel totalLiveTime;

	public NotifyPanel() {
		init();
	}

	private void init(){
		content = new JPanel();
		herb = new ImageIcon("img/herb/herb_logo.jpg");
		herbivore = new ImageIcon("img/herbivore/herbivore_logo.jpg");
		predator = new ImageIcon("img/predator/predator_logo.jpg");

		type = new JLabel();
		logo = new JLabel();
		generation = new JLabel();
		energy = new JLabel();
		liveTime = new JLabel();

		totalLiveTime = new JLabel();
		totalLiveTime.setFont(new Font("Arial", Font.BOLD, 35));

		buildPanel();
	}

	private void buildPanel(){

		FormLayout layout = new FormLayout(
				"10px, 25px, 92px",
				"20px, 3px, 140px, 3px, 20px,3px, 20px,3px,20px,3px,20px,3px,20px,3px,20px,3px,20px,3px,20px"+
				", 150px, 20px,3px,40px"
		);
//		DefaultFormBuilder builder = new DefaultFormBuilder(layout, new FormDebugPanel());
		DefaultFormBuilder builder = new DefaultFormBuilder(layout);
		CellConstraints cc = new CellConstraints();
		builder.addSeparator("Агент", cc.xyw(1, 1, 3));
		builder.addSeparator("Тип", cc.xyw(1, 5, 3));
		builder.add(type, cc.xyw(2, 7, 2));
		builder.addSeparator("Поколение", cc.xyw(1, 9, 3));
		builder.add(generation, cc.xyw(2, 11, 2));
		builder.addSeparator("Энергия", cc.xyw(1, 13, 3));
		builder.add(energy, cc.xyw(2, 15, 2));
		builder.addSeparator("Время жизни", cc.xyw(1, 17, 3));
		builder.add(liveTime, cc.xyw(2, 19, 2));
		builder.addSeparator("Прошло циклов", cc.xyw(1, 21, 3));
		builder.add(totalLiveTime, cc.xy(3, 23));

		builder.add(logo, cc.xyw(1, 3, 3));
		content.add(builder.getPanel());
	}

	@Override
	public void takt() {
		totalLiveTime.setText(String.valueOf(Constants.TOTAL_LIVE_TIME));
		if(agent != null){
			if(agent.isLive()){
				enablePanel();
				type.setText("Растение");
				generation.setText(String.valueOf(agent.getGeneration()));
				energy.setText(String.valueOf(agent.getEnergy()));
				liveTime.setText(String.valueOf(agent.getLiveTime()));
				agent.getImageIcon().setBorder(BorderFactory.createLineBorder(Color.YELLOW));
				logo.setBorder(BorderFactory.createTitledBorder("Логотип"));
				if(agent.getClass().equals(Herb.class)){
						logo.setIcon(herb);
						type.setText("Растение");
				}

				if(agent.getClass().equals(Herbivore.class)){
						logo.setIcon(herbivore);
						type.setText("Травоядное");
				}
				if(agent.getClass().equals(Predator.class)){
					logo.setIcon(predator);
					type.setText("Хищник");
			}
			}else{
				disablePanel();
			}
		}
		content.repaint();
		content.revalidate();
	}

	private void enablePanel(){
		logo.setEnabled(true);
		logo.setEnabled(true);
		type.setEnabled(true);
		generation.setEnabled(true);
		energy.setEnabled(true);
		liveTime.setEnabled(true);
	}

	private void disablePanel(){
		logo.setEnabled(false);
		logo.setEnabled(false);
		type.setEnabled(false);
		generation.setEnabled(false);
		energy.setEnabled(false);
		liveTime.setEnabled(false);
	}

	public JPanel getContent() {
		return content;
	}

	public void setContent(JPanel content) {
		this.content = content;
	}

	public static Agent agent;
}
